package com.pks.autoInsurance.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pks.autoInsurance.dto.LoginDto;
import com.pks.autoInsurance.dto.OwnerDetailsDto;
import com.pks.autoInsurance.dto.PersonalDetailsDto;
import com.pks.autoInsurance.dto.VehicleDetailsDto;
import com.pks.autoInsurance.helper.RegistrationHelper;

/**
 * Servlet implementation class RegistrationServlet 
 * Registration process of the user - 3 step process
 */
public class RegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println(getClass() + "::::doPost::::begin");
		String action = request.getParameter("action");
		if ("personal-details-register".equals(action))
			personalDetailsRegister(request, response);
		else if ("owner-details-register".equals(action))
			ownerDetailsRegister(request, response);
		else if ("vehicle-details-register".equals(action))
			vehicleDetailsRegister(request, response);
		System.out.println(getClass() + "::::doPost::::end");
	}

	/*
	 * Capture the registraion details entered on the 1st step of the user
	 * registration
	 */
	private void personalDetailsRegister(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			System.out.println(getClass()
					+ "::::personalDetailsRegister::::begin");
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String mobileNo = request.getParameter("mobileNo");
			String city = request.getParameter("city");
			String emailId = request.getParameter("emailId");
			String dateOfBirthString = request.getParameter("dateOfBirth");
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-dd");
			java.util.Date dateOfBirth = null;
			try {
				dateOfBirth = dateFormat.parse(dateOfBirthString);
				System.out.println("Nilesh date is" + dateOfBirth + "  "
						+ dateOfBirthString);
			} catch (ParseException e) {
				System.out.println("::::" + e.getMessage());
			}
			String userId = request.getParameter("userId");
			String address = request.getParameter("address");
			String passwd = request.getParameter("passwd");
			String gender = request.getParameter("gender");

			LoginDto loginDto = new LoginDto();
			loginDto.setUserId(userId);
			loginDto.setPasswd(passwd);
			RegistrationHelper registrationHelper = new RegistrationHelper();

			boolean userExistsFlag = registrationHelper
					.isExistingUser(loginDto);
			if (userExistsFlag) {
				request.setAttribute("error", "User ID already exists");
				request.getRequestDispatcher("register.jsp").forward(request,
						response);
			} else {
				PersonalDetailsDto personalDetailsDto = new PersonalDetailsDto();
				personalDetailsDto.setFirstName(firstName);
				personalDetailsDto.setLastName(lastName);
				personalDetailsDto.setMobileNo(mobileNo);
				personalDetailsDto.setCity(city);
				personalDetailsDto.setEmailId(emailId);
				personalDetailsDto.setDateOfBirth(dateOfBirth);
				personalDetailsDto.setGender(gender);
				personalDetailsDto.setUserId(userId);
				personalDetailsDto.setAddress(address);
				System.out.println("::::populating login dto::::" + loginDto);
				HttpSession session = request.getSession(false);
				System.out.println("::::populating personalDetailsDto:::::"
						+ personalDetailsDto);
				session.setAttribute("personalDetailsDto", personalDetailsDto);
				session.setAttribute("loginDto", loginDto);
				request.getRequestDispatcher("ownerDetail.jsp").forward(
						request, response);
			}
		} catch (ServletException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		} catch (IOException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		}
		System.out.println(getClass() + "::::personalDetailsRegister::::end");
	}

	/*
	 * Capture the registraion details entered on the 2nd step of the user
	 * registration
	 */
	private void ownerDetailsRegister(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			System.out
					.println(getClass() + "::::ownerDetailsRegister::::begin");
			String ssn = request.getParameter("ssn");
			String bloodGroup = request.getParameter("bloodGroup");
			String drivingLicence = request.getParameter("drivingLicence");
			String totalAccident = request.getParameter("totalAccident");
			OwnerDetailsDto ownerDetailsDto = new OwnerDetailsDto();
			ownerDetailsDto.setSsn(ssn);
			ownerDetailsDto.setDrivingLicence(drivingLicence);
			ownerDetailsDto.setTotalAccident(Integer.parseInt(totalAccident));
			ownerDetailsDto.setBloodGroup(bloodGroup);

			HttpSession session = request.getSession(false);
			PersonalDetailsDto personalDetailsDto = (PersonalDetailsDto) session
					.getAttribute("personalDetailsDto");
			System.out.println("::::ownerDetailsDto::::" + ownerDetailsDto);
			personalDetailsDto.setOwnerDetailsDto(ownerDetailsDto);
			session.setAttribute("personalDetailsDto", personalDetailsDto);
			request.getRequestDispatcher("vechileDetails.jsp").forward(request,
					response);

		} catch (ServletException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		} catch (IOException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		}
		System.out.println(getClass() + "::::ownerDetailsRegister::::end");

	}

	/*
	 * Capture the vehicle details of user entered on the 3rd step of the user
	 * registration
	 */
	private void vehicleDetailsRegister(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println(getClass() + "::::vehicleDetailsRegister::::begin");
		String type = request.getParameter("type");
		String model = request.getParameter("model");
		String make = request.getParameter("make");
		String vehicleRegistrationNo = request
				.getParameter("vehicleRegistrationNo");
		String policyType = request.getParameter("policyType");
		String mfYear = request.getParameter("mfYear");
		String policyAmount = request.getParameter("policyAmount");

		VehicleDetailsDto vehicleDetailsDto = new VehicleDetailsDto();
		vehicleDetailsDto.setType(type);
		vehicleDetailsDto.setModel(model);
		vehicleDetailsDto.setMake(make);
		vehicleDetailsDto.setVehicleRegistrationNo(vehicleRegistrationNo);
		vehicleDetailsDto.setPolicyType(policyType);
		vehicleDetailsDto.setMfYear(mfYear);
		vehicleDetailsDto.setPolicyAmount(Integer.parseInt(policyAmount));

		HttpSession session = request.getSession(false);
		PersonalDetailsDto personalDetailsDto = (PersonalDetailsDto) session
				.getAttribute("personalDetailsDto");
		LoginDto loginDto = (LoginDto) session.getAttribute("loginDto");
		System.out.println("::::vehicleDetailsDto::::" + vehicleDetailsDto);
		personalDetailsDto.setVehicleDetailsDto(vehicleDetailsDto);
		System.out
				.println(getClass()
						+ "::::vehicleDetailsRegister::::all data set to personalDetailsDto");
		RegistrationHelper registrationHelper = new RegistrationHelper();
		registrationHelper.registerUser(personalDetailsDto, loginDto);
		session.setAttribute("personalDetailsDto", null);
		session.setAttribute("loginDto", null);
		session.setAttribute("userId", personalDetailsDto.getUserId());
		try {
			request.setAttribute("quote", registrationHelper
					.getQuote((String) session.getAttribute("userId")));
			request.getRequestDispatcher("finalQuotation.jsp").forward(request,
					response);
		} catch (ServletException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		} catch (IOException e) {
			System.err.println(getClass() + "::::" + e.getMessage());
		}
		System.out.println(getClass() + "::::vehicleDetailsRegister::::end");
	}
}
